package com.wnxy.pawnbackgroundsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wnxy.pawnbackgroundsystem.dto.GetAllWareHousePageDto;
import com.wnxy.pawnbackgroundsystem.dto.GetAllWreHouseDto;
import com.wnxy.pawnbackgroundsystem.dto.SelectHouseDto;
import com.wnxy.pawnbackgroundsystem.dto.WareHouseDto;
import com.wnxy.pawnbackgroundsystem.entity.Store;
import com.wnxy.pawnbackgroundsystem.entity.StoreWarehouse;
import com.wnxy.pawnbackgroundsystem.entity.Warehouse;
import com.wnxy.pawnbackgroundsystem.dao.WarehouseMapper;
import com.wnxy.pawnbackgroundsystem.param.GetHouseParam;
import com.wnxy.pawnbackgroundsystem.param.UpdateHouseParam;
import com.wnxy.pawnbackgroundsystem.param.WarehouseParam;
import com.wnxy.pawnbackgroundsystem.service.IStoreService;
import com.wnxy.pawnbackgroundsystem.service.IStoreWarehouseService;
import com.wnxy.pawnbackgroundsystem.service.IWarehouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.naming.Name;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 仓库管理表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-10-14
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements IWarehouseService {
    @Autowired
    private  IStoreWarehouseService iStoreWarehouseService;
    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private IStoreService iStoreService;
    @Override
    public void delWarehouse(Integer houseId) {
        this.removeById(houseId);
        iStoreWarehouseService.remove(new QueryWrapper<StoreWarehouse>().eq("warehouse_id ", houseId+""));

    }

    @Override
    public void insertHouse(WarehouseParam wp) {
        Warehouse warehouse = new Warehouse();
        warehouse.setWarehouseName(wp.getWarehouseName());
        warehouse.setWarehousePeople(wp.getWarehousePeople());
        warehouse.setWarehousePhone(wp.getWarehousePhone());
        warehouse.setWarehouseStatus(wp.getWarehouseStatus());
        warehouse.setWarehouseAddress(wp.getWarehouseAddress());
        save(warehouse);
        List<Integer> num = wp.getNum();
        for (Integer integer : num) {
            StoreWarehouse storeWarehouse = new StoreWarehouse();
            storeWarehouse.setStoreId(integer+"");
            storeWarehouse.setWarehouseId(warehouse.getWarehouseId()+"");
            iStoreWarehouseService.save(storeWarehouse);
        }


    }


    @Override
    public List<WareHouseDto> getAllHouse() {
        List<WareHouseDto> wd=new ArrayList<>();
        List<Warehouse> list = this.list();
        for (Warehouse warehouse : list) {
            WareHouseDto wareHouseDto = new WareHouseDto();
            wareHouseDto.setKey(warehouse.getWarehouseId());
            wareHouseDto.setLabel(warehouse.getWarehouseName());
            wd.add(wareHouseDto);
        }
        return wd;
    }


    @Override
    public IPage<SelectHouseDto> houseDto(GetHouseParam getHouseParam) {
        HashMap<String, Object> map = new HashMap<>();
        if(null!=getHouseParam.getWarehouseStatus()){
            map.put("StoreStatus",getHouseParam.getWarehouseStatus());
        }
        if(null!=getHouseParam.getWarehousePhone()){
            map.put("StorePhone",getHouseParam.getWarehousePhone());
        }
        if (null!=getHouseParam.getWarehousePeople()){
            map.put("StorePeople",getHouseParam.getWarehousePeople());
        }
        if (null!=getHouseParam.getWarehouseName()){
            map.put("StoreName",getHouseParam.getWarehouseName());
        }
        map.put("Index",getHouseParam.getPageParam().getIndexPage());
        map.put("Size",getHouseParam.getPageParam().getSize());
        List<SelectHouseDto> house = warehouseMapper.house(map);
        Page<SelectHouseDto> storeDtoPage = new Page<SelectHouseDto>();
        storeDtoPage.setRecords(house);
        List<SelectHouseDto> selectHouseDtos = warehouseMapper.houseCunt(map);
        System.out.println("******");
        System.out.println(selectHouseDtos);
        storeDtoPage.setTotal(selectHouseDtos.size());
        storeDtoPage.setCurrent(getHouseParam.getPageParam().getIndexPage());
        storeDtoPage.setSize(getHouseParam.getPageParam().getSize());
        return storeDtoPage;

    }

    //查询仓库及其关联门店
    public GetAllWareHousePageDto getAllWareHouse(GetHouseParam getHouseParam){
        QueryWrapper<Warehouse> qw = new QueryWrapper<>();
        if(null!=getHouseParam.getWarehouseName()){
            qw.like("warehouse_name",getHouseParam.getWarehouseName());
        }
        if(null!=getHouseParam.getWarehousePhone()){
            qw.like("warehouse_phone",getHouseParam.getWarehousePhone());
        }
        if(null!=getHouseParam.getWarehousePeople()){
            qw.like("warehouse_people",getHouseParam.getWarehousePeople());
        }
        if(null!=getHouseParam.getWarehouseStatus()){
            qw.like("warehouse_status",getHouseParam.getWarehouseStatus());
        }

        Page<Warehouse> page = this.page(new Page<>(getHouseParam.getPageParam().getIndexPage(), getHouseParam.getPageParam().getSize()), qw);
        ArrayList<GetAllWreHouseDto> getAllWreHouseDtos = new ArrayList<>();
        for(Warehouse wr:page.getRecords()){
            GetAllWreHouseDto getAllWreHouseDto = new GetAllWreHouseDto();
            List<StoreWarehouse> storeWarehouses = iStoreWarehouseService.list(new QueryWrapper<StoreWarehouse>().eq("warehouse_id", wr.getWarehouseId()));
            StringBuilder sb = new StringBuilder();
            BeanUtils.copyProperties(wr,getAllWreHouseDto);
            for(StoreWarehouse sw:storeWarehouses){
                Store store = iStoreService.getOne(new QueryWrapper<Store>().eq("store_id", sw.getStoreId()));
                sb.append(store.getStoreName());
                sb.append(" ");
            }
            getAllWreHouseDto.setStores(sb.toString());
            getAllWreHouseDtos.add(getAllWreHouseDto);
        }
        GetAllWareHousePageDto getAllWareHousePageDto = new GetAllWareHousePageDto();
        getAllWareHousePageDto.setLg(getAllWreHouseDtos);
        getAllWareHousePageDto.setIndexPage(page.getCurrent());
        getAllWareHousePageDto.setTotle(page.getTotal());
//        getAllWareHousePageDto.setMaxPage(page.getMaxLimit());

        return getAllWareHousePageDto;
    }

    @Override
    public List<String> getStoreName(Integer warehouseId) {
        if(null!=warehouseId){
            ArrayList<String> name = new ArrayList<>();

            List<StoreWarehouse> storeWarehouses = iStoreWarehouseService.list(new QueryWrapper<StoreWarehouse>().eq("warehouse_id", warehouseId+""));
            for (StoreWarehouse storeWarehouse : storeWarehouses) {
                name.add(storeWarehouse.getStoreId()+"");
            }
            return name;
        }

        return null;
    }

    @Override
    public void updateHouse(UpdateHouseParam up) {
        if (up!=null){
            Warehouse warehouse= getOne(new QueryWrapper<Warehouse>().eq("warehouse_id ", up.getWarehouseId()));
            warehouse.setWarehouseName(up.getWarehouseName());
            warehouse.setWarehousePeople(up.getWarehousePeople());
            warehouse.setWarehousePhone(up.getWarehousePhone());
            warehouse.setWarehouseStatus(up.getWarehouseStatus());
            warehouse.setWarehouseAddress(up.getWarehouseAddress());
            this.saveOrUpdate(warehouse);
            iStoreWarehouseService.remove(new UpdateWrapper<StoreWarehouse>().eq("warehouse_id",warehouse.getWarehouseId()));
            for (Integer integer : up.getNum()) {
                StoreWarehouse storeWarehouse = new StoreWarehouse();
                storeWarehouse.setWarehouseId(warehouse.getWarehouseId()+"");
                storeWarehouse.setStoreId(integer+"");
                iStoreWarehouseService.save(storeWarehouse);
            }

        }

    }


}
